package com.manli.api.service;

import com.manli.api.dto.BuyCarLog;
import com.manli.api.pojo.QueryParamsVisitWantToBuyLog;
import com.manli.api.pojo.SessionInfo;
import com.manli.api.util.SessionInfoUtils;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.VisitWantToBuyLogDao;
import com.manli.api.dto.VisitWantToBuyLog;

import javax.servlet.http.HttpServletRequest;


@Service("visitWantToBuyLogService")
public class VisitWantToBuyLogService {
    @Autowired
    VisitWantToBuyLogDao visitWantToBuyLogDao;
    @Autowired
    SQLManager sqlManager;
    @Autowired
    SessionInfoUtils sessionInfoUtils;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from visitWantToBuyLog where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<VisitWantToBuyLog> pageQuery = new PageQuery<VisitWantToBuyLog>(params.getPage(),params.getLimit());
        PageQuery<VisitWantToBuyLog> query =sqlManager.execute(sqlReady,VisitWantToBuyLog.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<VisitWantToBuyLog> list = visitWantToBuyLogDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer visitWantToBuyLogId) {
        VisitWantToBuyLog visitWantToBuyLog = visitWantToBuyLogDao.single(visitWantToBuyLogId);
        return ResultUtil.success(visitWantToBuyLog);
    }

    @Transactional
    public Result save(VisitWantToBuyLog visitWantToBuyLog, HttpServletRequest request) {
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        VisitWantToBuyLog sourceVisitWantToBuyLog = visitWantToBuyLogDao.createLambdaQuery()
                .andEq("unionid",sessionInfo.getWeChatUserInfo().getUnionid())
                .andEq("goods_def_id" , visitWantToBuyLog.getGoodsDefId())
                .andEq("goods_model_id" , visitWantToBuyLog.getGoodsModelId())
                .appendSql(" and create_time > CURDATE() ").single();
        if(sourceVisitWantToBuyLog == null){
            visitWantToBuyLog.setOpenid(sessionInfo.getWeChatUserInfo().getOpenid());
            visitWantToBuyLog.setUnionid(sessionInfo.getWeChatUserInfo().getUnionid());
            visitWantToBuyLog.setStatus(1);
            visitWantToBuyLog.setCreateTime(new Date());
            visitWantToBuyLogDao.insert(visitWantToBuyLog);
        }else{
            sourceVisitWantToBuyLog.setBuyCount(visitWantToBuyLog.getBuyCount());
            sourceVisitWantToBuyLog.setStatus(1);
            visitWantToBuyLogDao.updateById(sourceVisitWantToBuyLog);
        }
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(VisitWantToBuyLog visitWantToBuyLog) {
        visitWantToBuyLogDao.updateTemplateById(visitWantToBuyLog);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(VisitWantToBuyLog visitWantToBuyLog) {
        visitWantToBuyLogDao.updateTemplateById(visitWantToBuyLog);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            visitWantToBuyLogDao.deleteById(id);
        }
        return ResultUtil.success();
    }


    public Result getVisitWantToBuyLogForToday(QueryParamsVisitWantToBuyLog queryParamVisitWantToBuyLog, HttpServletRequest request) {
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        SQLReady sqlBranchPartnerNo = null;
        String sql = "SELECT " +
                "gd.goods_def_id, " +
                "gm.goods_model_id, " +
                "gm.goods_model_sale_price, " +
                "gm.goods_model_channel_price, " +
                "gm.goods_model_source_price, " +
                "gd.goods_major_type, " +
                "vwtbl.buy_count, " +
                "b.branch_id, " +
                "p.partner_id," +
                "vsi.salesCount," +
                "vsi.stockCount " +
                "FROM " +
                "visit_want_to_buy_log vwtbl " +
                "LEFT JOIN goods_def gd ON vwtbl.goods_def_id = gd.goods_def_id " +
                "LEFT JOIN goods_model gm ON vwtbl.goods_model_id = gm.goods_model_id " +
                "LEFT JOIN branch b ON gd.branch_id = b.branch_id " +
                "LEFT JOIN partner p ON p.partner_id = gd.partner_id " +
                "LEFT JOIN view_stock_info vsi on vsi.goods_model_id = gm.goods_model_id " +
                "WHERE " +
                "vwtbl.unionid = ? " +
                (queryParamVisitWantToBuyLog.getBranchId() == null?" ":"AND b.branch_id = " + queryParamVisitWantToBuyLog.getBranchId() + " ") +
                "AND gd.`status` = 1 " +
                "AND gm.`status` = 1 " +
                "and vwtbl.status = 1 " +
                "and vwtbl.buy_count > 0 " +
                "AND vwtbl.create_time > CURDATE() ";
        if(queryParamVisitWantToBuyLog.getPartnerId() !=null){
            sql += "AND p.partner_id = ? " ;
            sqlBranchPartnerNo = new SQLReady(sql
                    , sessionInfo.getWeChatUserInfo().getUnionid(),queryParamVisitWantToBuyLog.getPartnerId());
        }else{
            sqlBranchPartnerNo = new SQLReady(sql
                    , sessionInfo.getWeChatUserInfo().getUnionid());
        }
        List<QueryParamsVisitWantToBuyLog> visitWantToBuyLogs = sqlManager.execute(sqlBranchPartnerNo,QueryParamsVisitWantToBuyLog.class);
        return ResultUtil.success(visitWantToBuyLogs);

    }
}
